package com.chuji.crmcj.adapter;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.volley.Response;
import com.chuji.crmcj.R;
import com.chuji.crmcj.act.ReceptionActivity;
import com.chuji.crmcj.act.SearchActivity;
import com.chuji.crmcj.bean.ConnCusInfo;
import com.chuji.crmcj.utils.SPUtils;
import com.chuji.crmcj.utils.UIUtils;
import com.chuji.crmcj.utils.UrlUtils;

import org.json.JSONObject;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by w.pitt on 2016/2/18.
 */
public class ConnAdapter extends CustomeAdapter {
    public List<ConnCusInfo.ApiParamObjEntity> connCusInfo;
    private ViewHolder holder;
    private Activity mContext;
    private String salerID;
    private String flowID;

    public ConnAdapter(Activity context, List<ConnCusInfo.ApiParamObjEntity> connCusInfo, String salerID, String flowID) {
        this.connCusInfo = connCusInfo;
        this.mContext = context;
        this.salerID = salerID;
        this.flowID = flowID;
    }

    @Override
    public int getCount() {
        return connCusInfo.isEmpty() ? 0 : connCusInfo.size();
    }

    @Override
    public Object getItem(int position) {
        return connCusInfo.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = View.inflate(UIUtils.getContext(), R.layout.item_relation, null);
            holder = new ViewHolder();
            holder.mTvName = (TextView) convertView.findViewById(R.id.tv_name);
            holder.mTvNumber = (TextView) convertView.findViewById(R.id.tv_number);
            holder.mIvConn = (ImageView) convertView.findViewById(R.id.iv_connclick);
            holder.mTvWantCar = (TextView) convertView.findViewById(R.id.tv_want_car);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        holder.mTvName.setText(connCusInfo.get(position).getName());
        holder.mTvNumber.setText(connCusInfo.get(position).getTel());
        holder.mTvWantCar.setText(connCusInfo.get(position).getCarModel());

        holder.mIvConn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showConnDialog(position);
            }
        });

        return convertView;
    }

    private void showConnDialog(final int position) {
        AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
        final AlertDialog dialog = builder.create();
        View view = View.inflate(mContext, R.layout.dialog_conn, null);// 给dialog设定特定布局
        // dialog.setView(view);
        dialog.setView(view, 0, 0, 0, 0);// 去掉上下左右边距, 兼容2.x版本
        Button mBtSave = (Button) view.findViewById(R.id.btn_sure);
        Button mBtCancel = (Button) view.findViewById(R.id.btn_cancel);
        mBtSave.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                reqConnCus(position);
                dialog.dismiss();
            }
        });
        mBtCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });
        dialog.show();
    }

    private void reqConnCus(final int position) {
//        String url = String.format(UrlUtils.RelatePassengerFlow + "&SalesID=%s&FlowID=%s&CustomerID=%s", salerID, flowID, connCusInfo.getApiParamObj().get(position).getCustomerID());
//        volleyRequest(url, new Response.Listener<String>() {
//            @Override
//            public void onResponse(String s) {
//                UIUtils.runInMainThread(new Runnable() {
//                    @Override
//                    public void run() {
//                        UIUtils.showToastSafe("已经成功关联客户");
//                        //关联成功后  添加到sp中  客户离店   将sp清空
//                        SPUtils.saveString(UIUtils.getContext(), "CustomerID", connCusInfo.getApiParamObj().get(position).getCustomerID());
//
//                        Intent intent = new Intent(mContext, ReceptionActivity.class);
//                        UIUtils.startActivity(intent);
//                    }
//                });
//            }
//        });

        String url = String.format(UrlUtils.RelatePassengerFlow);
        Map<String, String> map = new HashMap<String, String>();
        map.put("SalesID", salerID);
        map.put("FlowID", flowID);
        map.put("CustomerID", connCusInfo.get(position).getCustomerID());
        postRequest(url, map, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject jsonObject) {
                UIUtils.showToastSafe("已经成功关联客户");
                //关联成功后  添加到sp中  客户离店   将sp清空
                SPUtils.saveString(UIUtils.getContext(), "CustomerID", connCusInfo.get(position).getCustomerID());

                Intent intent = new Intent(mContext, ReceptionActivity.class);
                UIUtils.startActivity(intent);
                ((SearchActivity) mContext).finish();
            }
        });
    }

    public List<ConnCusInfo.ApiParamObjEntity> getList() {
        return connCusInfo;
    }

    static class ViewHolder {
        public TextView mTvName;
        public TextView mTvNumber;
        public ImageView mIvConn;
        public TextView mTvWantCar;
    }
}
